<?php

class Install {
	public static function makeStructure() {

		//checking the system structure...
		$dirs = Array (
					'system', 
					'system/config', 
					'system/dao', 
					'system/blc', 
					'system/modules', 
					'layout', 
					'layout/images', 
					'layout/javascript', 
					'layout/css', 
					'layout/third', 
					'layout/applet', 
					'layout/swf', 
					'layout/icons',
					'wrappers',
					'cache' 
		
		);
		
		foreach ( $dirs as $dir ) {
			$directory = SYSTEM_PATH . DIRECTORY_SEPARATOR . $dir;
			if (! is_dir ( $directory )) {
				mkdir ( $directory );
			}
			//chmod ( $directory, 0664 );
		}
		
		//coping third part javascript
		$dirs = Array ('prototype' );
		
		foreach ( $dirs as $dir ) {
			$source = IWC_PATH . DIRECTORY_SEPARATOR . '__third_javascript' . DIRECTORY_SEPARATOR . $dir;
			$dest = SYSTEM_PATH . DIRECTORY_SEPARATOR . 'layout' . DIRECTORY_SEPARATOR . 'third' . DIRECTORY_SEPARATOR . $dir;
			if (! is_dir ( $dest ))
				mkdir ( $dest );
			self::dircopy ( $source, $dest );
			//chmod ( $dest, 0664 );
		}
		
		//own 
		$source = IWC_PATH . DIRECTORY_SEPARATOR . '__hibrid_javascript';
		$dest = SYSTEM_PATH . DIRECTORY_SEPARATOR . 'layout' . DIRECTORY_SEPARATOR . 'javascript';
		
		self::dircopy ( $source, $dest );
		//chmod ( $dest, 0664 );
	
	}
	
	private static function dircopy($src_dir, $dst_dir, $verbose = false, $use_cached_dir_trees = false) {
		static $cached_src_dir;
		static $src_tree;
		static $dst_tree;
		$num = 0;
		
		if (($slash = substr ( $src_dir, - 1 )) == "\\" || $slash == "/")
			$src_dir = substr ( $src_dir, 0, strlen ( $src_dir ) - 1 );
		if (($slash = substr ( $dst_dir, - 1 )) == "\\" || $slash == "/")
			$dst_dir = substr ( $dst_dir, 0, strlen ( $dst_dir ) - 1 );
		
		if (! $use_cached_dir_trees || ! isset ( $src_tree ) || $cached_src_dir != $src_dir) {
			$src_tree = self::getDirectoryTree ( $src_dir );
			$cached_src_dir = $src_dir;
			$src_changed = true;
		}
		if (! $use_cached_dir_trees || ! isset ( $dst_tree ) || $src_changed)
			$dst_tree = self::getDirectoryTree ( $dst_dir );
		if (! is_dir ( $dst_dir ))
			mkdir ( $dst_dir, 0664, true );
		
		foreach ( $src_tree as $file => $src_mtime ) {
			if (! isset ( $dst_tree [$file] ) && $src_mtime === false) // dir
				mkdir ( "$dst_dir/$file" ); elseif (! isset ( $dst_tree [$file] ) && $src_mtime || isset ( $dst_tree [$file] ) && $src_mtime > $dst_tree [$file]) // file
{
				if (copy ( "$src_dir/$file", "$dst_dir/$file" )) {
					if ($verbose)
						echo "Copied '$src_dir/$file' to '$dst_dir/$file'<br>\r\n";
					touch ( "$dst_dir/$file", $src_mtime );
					$num ++;
				} else
					echo "<font color='red'>File '$src_dir/$file' could not be copied!</font><br>\r\n";
			}
		}
		
		return $num;
	}
	
	/* Creates a directory / file tree of a given root directory
     *
     * @param $dir str Directory or file without ending slash
     * @param $root bool Must be set to true on initial call to create new tree.
     * @return Directory & file in an associative array with file modified time as value.
     */
	private static function getDirectoryTree($dir, $root = true) {
		static $tree;
		static $base_dir_length;
		
		if ($root) {
			$tree = array ( );
			$base_dir_length = strlen ( $dir ) + 1;
		}
		
		if (is_file ( $dir )) {
			//if (substr($dir, -8) != "/CVS/Tag" && substr($dir, -9) != "/CVS/Root"  && substr($dir, -12) != "/CVS/Entries")
			$tree [substr ( $dir, $base_dir_length )] = filemtime ( $dir );
		} elseif (is_dir ( $dir ) && $di = dir ( $dir )) // add after is_dir condition to ignore CVS folders: && substr($dir, -4) != "/CVS"
{
			if (! $root)
				$tree [substr ( $dir, $base_dir_length )] = false;
			while ( ($file = $di->read ()) !== false )
				if ($file != "." && $file != "..")
					self::getDirectoryTree ( "$dir/$file", false );
			$di->close ();
		}
		
		if ($root) {
			return $tree;
		}
	}
	public static function copyErrors(){
		$epath=SYSTEM_PATH.DIRECTORY_SEPARATOR.'errors';
		if(!is_dir($epath)){
			mkdir($epath);
			
			$generic=IWC_PATH.DIRECTORY_SEPARATOR.'__errors'.DIRECTORY_SEPARATOR.'generic.html';
			copy($generic,$epath.DIRECTORY_SEPARATOR.'generic.html');
		}
		
	}
	public static function getTrivialHtaccessProperties() {
		return '<IfModule mod_php5.c>'.NEW_LINE.
			   TAB.'php_flag register_globals off'.NEW_LINE.
			   TAB.'php_flag magic_quotes_gpc off'.NEW_LINE.
			   TAB.'php_flag asp_tags off'.NEW_LINE.
			   TAB.'php_flag short_open_tag off'.NEW_LINE.
			   TAB.'php_value post_max_size 80M'.NEW_LINE.
			   TAB.'php_value upload_max_filesize 80M'.NEW_LINE.
			   TAB.'php_value default_charset utf-8'.NEW_LINE.
			   '</IfModule>'.NEW_LINE.
			   'ErrorDocument 404 '.SYSTEM_URL.'/errors/generic.html'.NEW_LINE.
			   'ErrorDocument 403 '.SYSTEM_URL.'/errors/generic.html'.NEW_LINE.
			   'ErrorDocument 400 '.SYSTEM_URL.'/errors/generic.html'.NEW_LINE;
	}
	public static function denyDirectoryBrowse($dir){
		file_put_contents(SYSTEM_PATH.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.'.htaccess',
			   'order allow,deny'.NEW_LINE.'deny from all'.NEW_LINE);
	}
	public function createRootHtaccessFile() {
		file_put_contents(SYSTEM_PATH.DIRECTORY_SEPARATOR.'.htaccess',self::getTrivialHtaccessProperties());
	}
}

?>
